import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter);
const router = new VueRouter({
    routes: [
        {path: '/',redirect: '/index'},
        {path: '/login',component: resolve => require(['@/components/login.vue'], resolve), meta: {title: '登陆'}},
        {path: '/fail',component: resolve => require(['../components/common/failure.vue'], resolve)},
        {path: '/404',component: resolve => require(['../components/common/404.vue'], resolve)},
        {path: '/403',component: resolve => require(['../components/common/403.vue'], resolve)},
        {path: '/mainHeader',component: resolve => require(['../components/common/mainHeader.vue'], resolve)},
        {path: '/vipUser',component: resolve => require(['../components/vipUser.vue'], resolve)},
        {path: '/barCode',component: resolve => require(['../components/barCode.vue'], resolve)},
        {path: '*',redirect: '/404'}
    ],
    mode: 'history'
});

//全局路由守卫
router.beforeEach((to, from, next) => {
    console.log('跳转到:', to.fullPath);
    var token = sessionStorage.getItem('token');
    //如果没登录,都导向登录页
    if(!token) {
        if(to.path !== '/login') {
            next({
                path: '/login'
            })
        } else {
            next();
        }
    } else {
        next();
    }
})

export default router
